package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.ProcessDiffPayRecord;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * Description:
 *
 * @author Yanxinhao on 2025/2/13
 */
@Component(value = "1003")
@Slf4j
public class ProcessVipTypePayRecord implements ProcessDiffPayRecord {

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 处理消费类型是购买VIP的
     * 且用户成功支付的后置操作
     * @param userPaidRecordVo 用户支付记录
     */
    @Override
    public void processPayRecord(UserPaidRecordVo userPaidRecordVo) {
        //向user_vip_service表中插入数据
        String orderNo = userPaidRecordVo.getOrderNo();
        Long userId = userPaidRecordVo.getUserId();

        UserVipService userVipService = userVipServiceMapper.selectOne(new LambdaQueryWrapper<UserVipService>().eq(UserVipService::getOrderNo, orderNo));
        if (userVipService != null) {
            throw new GuiguException(201, "该vip类型订单支付流水已存在");
        }

        //1.获取用户充值vip的id类型,我们需要通过id获取VipServiceConfig对象,才能知道用户充值的日期是多久的
        Long vipId = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig config = vipServiceConfigMapper.selectById(vipId);
        if (config == null) {
            throw new GuiguException(201, "该套餐信息不存在");
        }

        //2.根据vip类型获取vip的续期时长信息
        Integer serviceMonth = config.getServiceMonth();

        //3.根据用户id获取用户对象,判断用户当前是否是vip
        UserInfo userInfo = userInfoMapper.selectById(userId);
        Integer isVip = userInfo.getIsVip();
        Date vipExpireTime = userInfo.getVipExpireTime();

        //4.现在我们需要根据用户的身份,以及vip的套餐时长,来判断UserVipService的过期时间设置多久
        UserVipService userVipService1 = new UserVipService();
        userVipService1.setOrderNo(orderNo);
        userVipService1.setUserId(userId);

        Calendar instance = Calendar.getInstance();
        //如果不是vip
        if (isVip == 0){
            instance.setTime(new Date());
        } else {
            //如果是vip,但是已经到了过期时间
            if (vipExpireTime.before(new Date())){
            instance.setTime(new Date());
            } else {
                //是vip,且还没过期
                instance.setTime(vipExpireTime);
            }
        }
        //开始时间统一设置成现在
        userVipService1.setStartTime(new Date());

        //5.设置用户vip的过期时间
        instance.add(Calendar.MONTH, serviceMonth);
        userVipService1.setExpireTime(instance.getTime());

        //6.保存到数据库
        int insert = userVipServiceMapper.insert(userVipService1);
        log.info("保存订单：{}，记录支付流水:{}", orderNo, insert > 1 ? "成功" : "失败");

        //7.记得将用户的身份改为是vip
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService1.getExpireTime());
        int count = userInfoMapper.updateById(userInfo);
        log.info("修改用户信息{}", count > 1 ? "成功" : "失败");
    }
}